/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package managedbeans;

import entities.Forumblocks;
import entities.Forumthreads;
import java.io.Serializable;
import java.security.SecureRandom;
import java.util.Date;
import java.util.Random;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import utils.dataaccess.DBInterface;

/**
 *
 * @author Dezmond
 */
@SessionScoped
public class GenericForumBean implements Serializable{

    protected Forumthreads currentThread;
    protected Forumblocks currentBlock;

    public Forumthreads getCurrentThread() {
        currentThread = (Forumthreads) FacesContext.getCurrentInstance()
                         .getExternalContext()
                         .getSessionMap()
                         .get("tid");
        return currentThread;
    }

    protected void setCurrentThread(Integer threadID) {
        FacesContext.getCurrentInstance()
                     .getExternalContext()
                     .getSessionMap()
                     .remove("tid");
        FacesContext.getCurrentInstance()
                     .getExternalContext()
                     .getSessionMap()
                     .put("tid", DBInterface.getThreadById(threadID));
    }

    protected void setCurrentThread(Forumthreads thread) {
        FacesContext.getCurrentInstance()
                     .getExternalContext()
                     .getSessionMap()
                     .remove("tid");
        FacesContext.getCurrentInstance()
                     .getExternalContext()
                     .getSessionMap()
                     .put("tid", thread);
    }

    public Forumblocks getCurrentBlock() {
        currentBlock = (Forumblocks) FacesContext.getCurrentInstance()
                         .getExternalContext()
                         .getSessionMap()
                         .get("bid");
        return currentBlock;
    }

    protected void setCurrentBlock(Integer blockID) {
         FacesContext.getCurrentInstance()
                     .getExternalContext()
                     .getSessionMap()
                     .remove("bid");
         FacesContext.getCurrentInstance()
                     .getExternalContext()
                     .getSessionMap()
                     .put("bid", DBInterface.getBlockById(blockID));
    }

    protected void setCurrentBlock(Forumblocks block) {
         FacesContext.getCurrentInstance()
                     .getExternalContext()
                     .getSessionMap()
                     .remove("bid");
         FacesContext.getCurrentInstance()
                     .getExternalContext()
                     .getSessionMap()
                     .put("bid", block);
    }

    protected void showMessage(String title, String message){
        FacesMessage fmessage = new FacesMessage(title, message);
        FacesContext.getCurrentInstance().addMessage(null, fmessage);
    }

    public String retrieveAvatarPath(String username){
        return "images/avatars/"+username+".png";
    }
    
    protected Date getTimestamp(){
        return new Date();
    }

    protected int generateID(){
        Random random = new SecureRandom();
        return ((random.nextInt() + random.nextInt()) / (random.nextInt(20)+1));
    }

    

}
